package com.gem.growingtree.bodyline;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class BodyDataService {
	private MyDatabaseHelper myDatabaseHelper;

	public BodyDataService(Context context) {
		myDatabaseHelper = new MyDatabaseHelper(context);
	}

	public void addData(BodyData contact) throws Exception {
		SQLiteDatabase sd = myDatabaseHelper.getWritableDatabase();
		sd.execSQL("insert into body_info(body_x,body_y) values(?,?)",
				new Object[] { contact.getX(), contact.getY() });
//		 ContentValues values = new ContentValues();
//		 values.put("body_x", xx);
//		 values.put("body_y", bmi);
//		 long n = db.insert("body_info", null, values);
		sd.close();
	}

	public List<BodyData> getData() {
		SQLiteDatabase sd = myDatabaseHelper.getReadableDatabase();
		List<BodyData> list = new ArrayList<BodyData>();
		Cursor cursor = sd.rawQuery("select * from body_info", null);
		while (cursor.moveToNext()) {
			//取得下标
//			Integer a = cursor.getColumnIndex("body_x");
//			Float b = (float) cursor.getColumnIndex("body_y");
			Integer a=cursor.getInt(1);
			Float b=cursor.getFloat(2);
			BodyData bodyData=new BodyData(a, b);
			list.add(bodyData);
		}
		return list;
	}
}
